package com.hot100;

public class Solution42 {
    public int trap(int[] height) {
        int n = height.length;
        int[] maxLeft = new int[n];
        maxLeft[0] = 0;
        for (int i = 1; i < n; i++) {
            maxLeft[i] = Math.max(maxLeft[i - 1], height[i - 1]);
        }
        int[] maxRight = new int[n];
        maxRight[n - 1] = 0;
        for (int i = n - 2; i >= 0; i--) {
            maxRight[i] = Math.max(maxRight[i + 1], height[i + 1]);
        }
        int res = 0;
        for (int i = 1; i < n - 1; i++) {
            int h = Math.max(0, Math.min(maxLeft[i], maxRight[i]) - height[i]);
            res += h;
        }
        return res;
    }
}
